package com.xiao.hao.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", () -> LocalDateTime.now(),
                LocalDateTime.class);
        this.strictUpdateFill(metaObject, "updateTime", () -> LocalDateTime.now(),
                LocalDateTime.class);
        this.strictInsertFill(metaObject, "createBy", () -> UserConfig.getUserId(),
                Long.class);
        this.strictInsertFill(metaObject, "createByName", () -> UserConfig.getUserName(),
                String.class);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateByName", null);
        metaObject.setValue("updateBy", null);
        metaObject.setValue("updateTime", null);
        this.strictUpdateFill(metaObject, "updateTime", () -> LocalDateTime.now(),
                LocalDateTime.class);
        this.strictUpdateFill(metaObject, "updateBy", () -> UserConfig.getUserId(),
                Long.class);
        this.strictInsertFill(metaObject, "updateByName", () -> UserConfig.getUserName(),
                String.class);
    }
}
